//
//  TimeRuleViewController.m
//  SenMo
//
//  Created by ZengYuan on 15/12/29.
//  Copyright © 2015年 ZengYuan. All rights reserved.
//

#import "TimeRuleViewController.h"
#import "DeviceHandle.h"

@interface TimeRuleViewController()
{
    UIDatePicker *datePicker;
    
}
@end

@implementation TimeRuleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.titleLabel.text = @"开放时间";
    
    
    if (self.start_time && self.end_time) {
         self.startLabel.text = [NSString stringWithFormat:@"开始时间：%@",self.start_time];
        
        self.endLabel.text = [NSString stringWithFormat:@"结束时间：%@",self.end_time];

    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)sure:(id)sender{
    
    if(self.start_time && self.end_time){
        [DeviceHandle deviceSetTimeInterval:self.device_unit.device_id start_time:self.start_time end_time:self.end_time success:^{
            MBALERT(@"设置成功");
        } failure:^(NSString *error) {
            MBALERT(error);
        }];
    }else{
        MBALERT(@"请设置完整的时间段");
    }
    
}

-(IBAction)selTimeAction:(UIButton *)sender{
    
    if (sender.tag == 100) {
        selStartTime = YES;
    }else{
        selStartTime = NO;
    }
    
    [self openPicker];
}


#pragma mark - 选择器

-(void)addPickerView{
    
    static float pick_y = 50.;
    
        if (!datePicker) {
            datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, pick_y, 320, 200)];
            datePicker.datePickerMode = UIDatePickerModeTime;
//            datePicker.maximumDate = [NSDate date];
        }
        
        NSString *dateStr=nil;
    
        
        NSDate *curdate=[SystemUtils dateByString:dateStr];
        if (!curdate) {
            curdate = [NSDate date];
        }
        [datePicker setDate:curdate];
        
        [chooseTimeView addSubview:datePicker];
 }

-(void)openPicker
{
    UIWindow *window=[[UIApplication sharedApplication].windows lastObject];
    
    if (!chooseTimeView) {
        closeTimeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        [closeTimeButton addTarget:self action:@selector(actionCancelButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        closeTimeButton.backgroundColor = [UIColor blackColor];
        closeTimeButton.alpha = 0.0;
        
        chooseTimeView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, 260)];
        chooseTimeView.backgroundColor = [UIColor whiteColor];
        
        //取消
        UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(8, 6, 60, 38)];
        cancelButton.titleLabel.font = [UIFont boldSystemFontOfSize:17];
        [cancelButton setTitleColor:RGB(142, 142, 147) forState:UIControlStateNormal];
        [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [cancelButton addTarget:self action:@selector(actionCancelButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [chooseTimeView addSubview:cancelButton];
        
        //完成
        UIButton *okButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 70, 6, 60, 38)];
        okButton.titleLabel.font = [UIFont boldSystemFontOfSize:17];
        [okButton setTitleColor:RGB(0, 162, 230) forState:UIControlStateNormal];
        [okButton setTitle:@"完成" forState:UIControlStateNormal];
        [okButton addTarget:self action:@selector(actionOKButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [chooseTimeView addSubview:okButton];
        
        
        [self addPickerView];
    }
    
    closeTimeButton.alpha = 0;
    [window addSubview:closeTimeButton];
    [window addSubview:chooseTimeView];
    
    
    CGRect chooseFrame = chooseTimeView.frame;
    chooseFrame.origin.y -= chooseFrame.size.height;
    [UIView animateWithDuration:0.3
                     animations:^(void){
                         chooseTimeView.frame = chooseFrame;
                         closeTimeButton.alpha = 0.4;
                     }
                     completion:^(BOOL finished){
                         
                     }];
    
}


- (void)actionCancelButtonClicked:(id)sender
{
    
    CGRect chooseFrame = chooseTimeView.frame;
    chooseFrame.origin.y = kScreenHeight;
    
    [UIView animateWithDuration:0.3
                     animations:^(void){
                         chooseTimeView.frame = chooseFrame;
                         closeTimeButton.alpha = 0;
                         
                     }
                     completion:^(BOOL finished){
                   
                         [datePicker removeFromSuperview];
                         datePicker=nil;
                         
                         [chooseTimeView removeFromSuperview];
                         chooseTimeView=nil;
                         
                         [closeTimeButton removeFromSuperview];
                         closeTimeButton=nil;
                         
                     }];
    
}

-(void)actionOKButtonClicked:(id)sender{
    [closeTimeButton removeFromSuperview];
    closeTimeButton=nil;
    
    
 
        NSDate *date = datePicker.date;
        NSDateFormatter* formater = [[NSDateFormatter alloc] init];
        [formater setDateFormat:@"HH:mm"];
        NSString*dateStr = [formater stringFromDate:date];
    

    if (selStartTime) {
        self.start_time = [[NSString alloc] initWithString:dateStr];
        self.startLabel.text = [NSString stringWithFormat:@"开始时间：%@",dateStr];
     }else{
         self.end_time = [[NSString alloc] initWithString:dateStr];
        self.endLabel.text   = [NSString stringWithFormat:@"结束时间：%@",dateStr];
         
    }
    
//        //生日
//        if (picker_type==BIRTH_PICKER) {
//            bbinfo.bb_bday = dateStr;
//        }//入园时间
//        else if(picker_type==ENROLL_PICKER){
//            bbinfo.bb_edate = dateStr;
//        }
//        [tableView reloadData];
    
 
    //    visitTimeLabel.text = [DataTime getStringFromDate:date];
    
    CGRect chooseFrame = chooseTimeView.frame;
    chooseFrame.origin.y = kScreenHeight;
    
    [UIView animateWithDuration:0.2
                     animations:^(void){
                         chooseTimeView.frame = chooseFrame;
                         closeTimeButton.alpha = 0;
                         
                     }
                     completion:^(BOOL finished){
          
                         
                         [datePicker removeFromSuperview];
                         datePicker=nil;
                         
                         [chooseTimeView removeFromSuperview];
                         chooseTimeView=nil;
                         
                         [closeTimeButton removeFromSuperview];
                         closeTimeButton=nil;
                         
                     }];
    
    
}

//-(void)setStart_time:(NSString *)start_time{
//    _start_time = start_time;
//}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
